package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.RepairDao;
import com.gable.exhibition.dao.UpkeepDao;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.SmsView;
import com.gable.exhibition.view.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维修或保养未通过
 * Created by mj on 2018/3/30.
 */
public class HospitalNotConfirmSmsThread implements Runnable{

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private Long resourcesId;

    private Integer resourcesType;      //数据类型 1:报修，2：保养

    private UserDao userDao;

    private RepairDao repairDao;

    private UpkeepDao upkeepDao;

    private String repairByDoctorUrl;

    private String upkeepByDoctorUrl;

    public HospitalNotConfirmSmsThread(Long resourcesId, Integer resourcesType, UserDao userDao,
                                    RepairDao repairDao, UpkeepDao upkeepDao,String repairByDoctorUrl,String upkeepByDoctorUrl){
        this.resourcesId = resourcesId;
        this.resourcesType = resourcesType;
        this.userDao = userDao;
        this.repairDao = repairDao;
        this.upkeepDao = upkeepDao;
        this.repairByDoctorUrl = repairByDoctorUrl;
        this.upkeepByDoctorUrl = upkeepByDoctorUrl;
    }

    @Override
    public void run() {
        try {
            SmsView smsView = null;
            String url = "";
            if(resourcesType == 1){
                smsView = repairDao.repairForSms(resourcesId);
                url = repairByDoctorUrl;
            }else if(resourcesType == 2){
                smsView = upkeepDao.upkeepForSms(resourcesId);
                url = upkeepByDoctorUrl;
            }

            String hospitalName = smsView.getHospitalName();
            String deviceName = smsView.getDeviceName();
            String sn = smsView.getSn();

            UserView userView = new UserView();
            userView.setTag(1007L); //平台管理员
            List<String> mobiles = userDao.queryUserListByTag(userView);

            //发送给服务商管理员
            if(mobiles != null && mobiles.size() > 0){
                try {
                    StringBuilder sb = new StringBuilder();
                    for(String phone : mobiles){
                        if(StringUtil.isEmpty(phone))
                            continue;
                        sb.append(phone).append(",");
                    }
                    String mobile = sb.substring(0, sb.length() - 1);
                    String message = hospitalName+"%&"+deviceName+"-"+sn;
                    Map<String,String> serverMap = new HashMap<String,String>();
                    serverMap.put("mobile",mobile);
                    serverMap.put("message",message);
                    String jsonResult = HttpHelper.postRequestResponseBodyAsString(url,serverMap);
                    logger.info("医院确认未通过给平台的短信，设备名称："+deviceName+",短信发送结果:"+jsonResult);
                } catch (Exception e) {
                    logger.info("医院确认未通过给平台的短信，设备名称："+deviceName+",短信发送失败:"+e);
                }
            }
        } catch (Exception e) {
            logger.info("医院确认未通过给平台短信异常："+e);
        }
    }
}
